import 'package:flutter/cupertino.dart';
import 'package:tiktokadult/assets_tool/app_colors.dart';
import 'package:video_player/video_player.dart';

class VideoBufferLoadingWidget extends StatefulWidget {
  final VideoPlayerController? controller;
  const VideoBufferLoadingWidget(this.controller, {super.key});

  @override
  State<StatefulWidget> createState() {
    return _VideoBufferLoadingWidgetState();
  }
}

class _VideoBufferLoadingWidgetState extends State<VideoBufferLoadingWidget> {
  @override
  void initState() {
    super.initState();
    widget.controller?.addListener(_listenerCallback);
  }

  @override
  void didUpdateWidget(covariant VideoBufferLoadingWidget oldWidget) {
    if (oldWidget.controller != widget.controller) {
      oldWidget.controller?.removeListener(_listenerCallback);
      widget.controller?.addListener(_listenerCallback);
    }
    super.didUpdateWidget(oldWidget);
  }

  void _listenerCallback() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (widget.controller?.value.isInitialized == true && widget.controller?.value.isBuffering == true) {
      return const Center(
        child: CupertinoActivityIndicator(
          color: AppColors.actionRed,
          radius: 15,
        ),
      );
    }
    return const SizedBox();
  }

  @override
  void dispose() {
    widget.controller?.removeListener(_listenerCallback);
    super.dispose();
  }
}
